# -*- coding: utf-8 -*-
import KBEngine
from KBEDebug import *
import s_profession
import ConstantDefine
import Utils


class CharacterPropertys:
    """角色属性接口"""

    def __int__(self):
        self._maxHp = 0
        self._maxMp = 0
        self._hp = 0  # 当前生命:
        self._mp = 0  # 目前的法力ֵ
        self._attackMin = 0  # 物理攻击力
        self._attackMax = 0  # 物理攻击力
        self._attackDefMin = 0  # 物理防御
        self._attackDefMax = 0  # 物理防御
        self._mcAttackMin = 0  # 魔法攻击力
        self._mcAttackMax = 0  # 魔法攻击力
        self._mcAttackDefMin = 0  # 魔法防御
        self._mcAttackDefMax = 0  # 魔法防御
        self._scAttackMin = 0  # 道术攻击
        self._scAttackMax = 0  # 道术攻击
        self._hit = 0  # 命中
        self._dodge = 0  # 闪避
        self._cri = 0  # 暴击几率
        self._toughness = 0  # 韧性 防暴击几率
        self._criHarm = 0  # 职业暴击伤害系数
        self._energy = energy  # 当前活力
        self._rank = 0  # 竞技排行
        self._luckAtk = 0  # 角色攻击幸运值
        self._luckDef = 0  # 角色诅咒值
        self._fightPower = 0  # 角色战斗力
        self._fightSpeed = 0  # 攻速
        self._timestamp = time.time()

    def calculateFightPower(self):
        """计算战斗力"""
        attack = {1: self._attackMin + self._attackMax, 2: self._scAttackMin + self._scAttackMax,
                  3: self._mcAttackMin + self._mcAttackMax, }.get(self.getProfession(), 0)
        defAtttack = self._attackDefMin + self._attackDefMax + self._mcAttackDefMin + self._mcAttackDefMax
        fightPower = int(
            attack * 8 + defAtttack * 4 + self._maxHp * 1 + self._maxMp * 0.5 + self._hit * 0.5 + self._dodge * 3.2 +
            self._cri * 2.7 + self._toughness * 4 + self._fightSpeed * 1.7)
        self.updateFightPower(fightPower)

        #         print u'人物战斗力为:%d'%fightPower

    # ===================获取当前等级一级基础属性================================
    def getExpEff(self):
        """获取经验获取百分比"""
        effectExpEff = 1  # self._owner.effect.getEffectInfo().get('ExpEff',0)
        return effectExpEff

    # ========================================================

    def getHp(self):
        """获取角色当前血量"""
        if self._hp > self.getMaxHp():
            self.updateHp(self.getMaxHp())
        return self._hp

    def setHp(self, hp):
        """设置角色血量值"""
        maxhp = self.getMaxHp()
        if hp > maxhp:
            self._hp = maxhp
        else:
            self._hp = hp

    def updateHp(self, hp, state=1):
        """更新角色血量值"""
        maxhp = self.getMaxHp()
        if hp > maxhp:
            hp = maxhp
        elif hp <= 0:
            hp = 1
        self._hp = hp

    def addHp(self, hp):
        """加血"""
        self.updateHp(self.getHp() + hp)

    def Restoration(self):
        """恢复角色满状态"""
        self.setHp(self.getMaxHp())
        self.setMp(self.getMaxMp())

    def getMp(self):
        """获取角色当前魔力值"""
        return self._mp

    def setMp(self, mp):
        """设置角色魔力值"""
        self._mp = mp

    def updateMp(self, mp):
        """更新角色魔力值"""
        maxmp = self.getMaxMp()
        if mp > maxmp:
            mp = maxmp
        elif mp < 0:
            mp = 0
        self._mp = mp

    def addMp(self, mp):
        """加蓝"""
        self.updateMp(self.getMp() + mp)

    def getEnergy(self):
        """获取角色当前活力值/熔炼值"""
        return self._energy

    def setEnergy(self, energy):
        """设置角色活力/熔炼值
        """
        if energy > self.MAXENERGY:
            self._energy = self.MAXENERGY
        elif energy < 0:
            self._energy = 0
        else:
            self._energy = energy

    def addEnergy(self, energy):
        """加活力/熔炼值"""
        nowenergy = self._energy + energy
        self.setEnergy(nowenergy)
        self._owner.SyncRoleEnergy()  # 同步熔炼值到客户端

    def checkEnoughEnergy(self, energy):
        """检测熔铸值是否足够"""
        if self._energy < energy:
            return False

        return True

    def setMaxHp(self, hp):
        """设置角色魔力值"""
        self._maxHp = hp

    def getMaxHp(self):
        """计算当前最大HP"""
        return self._maxHp

    def setMaxMp(self, mp):
        """设置角色魔力值"""
        self._maxMp = mp

    def getMaxMp(self):
        """计算当前最大MP"""
        return self._maxMp

    def setAttackMin(self, atk):
        """设置角色物理攻击力下限"""
        self._attackMin = atk

    def getAttackMin(self):
        """获取角色物理攻击力下限"""
        return self._attackMin

    def setAttackMax(self, atk):
        """设置角色物理攻击力上限"""
        self._attackMax = atk

    def getAttackMax(self):
        """获取角色物理攻击力上限"""
        return self._attackMax

    def setAttackDefMin(self, atkdef):
        """设置角色物理防御下限"""
        self._attackDefMin = atkdef

    def getAttackDefMin(self):
        """获取角色物理防御下限"""
        return self._attackDefMin

    def setAttackDefMax(self, atkdef):
        """设置角色物理防御上限"""
        self._attackDefMax = atkdef

    def getAttackDefMax(self):
        """获取角色物理防御上限"""
        return self._attackDefMax

    def setMcAttackMin(self, mcatk):
        """设置角色魔法攻击力下限"""
        self._mcAttackMin = mcatk

    def getMcAttackMin(self):
        """获取角色魔法攻击力下限"""
        return self._mcAttackMin

    def setMcAttackMax(self, mcatk):
        """设置角色魔法攻击力上限"""
        self._mcAttackMax = mcatk

    def getMcAttackMax(self):
        """获取角色魔法攻击力上限"""
        return self._mcAttackMax

    def setMcAttackDefMin(self, mcatkdef):
        """设置角色魔法防御下限"""
        self._mcAttackDefMin = mcatkdef

    def getMcAttackDefMin(self):
        """获取角色魔法防御下限"""
        return self._mcAttackDefMin

    def setMcAttackDefMax(self, mcatkdef):
        """设置角色魔法防御上限"""
        self._mcAttackDefMax = mcatkdef

    def getMcAttackDefMax(self):
        """获取角色魔法防御上限"""
        return self._mcAttackDefMax

    def setScAttackMin(self, scatk):
        """设置角色道术攻击力下限"""
        self._scAttackMin = scatk

    def getScAttackMin(self):
        """获取道术攻击力下限"""
        return self._scAttackMin

    def setScAttackMax(self, scatk):
        """设置角色道术攻击力上限"""
        self._scAttackMax = scatk

    def getScAttackMax(self):
        """获取道术攻击力上限"""
        return self._scAttackMax

    def setHit(self, hit):
        """设置角色命中"""
        self._hit = hit

    def getHit(self):
        """获取角色命中"""
        return self._hit

    def setDodge(self, doge):
        """设置角色躲闪"""
        self._dodge = doge

    def getDodge(self):
        """获取角色躲闪"""
        return self._dodge

    def setCri(self, cri):
        """设置角色暴击几率"""
        self._cri = cri

    def getCri(self):
        """获取角色暴击几率"""
        return self._cri

    def setToughness(self, tes):
        """设置角色韧性"""
        self._toughness = tes

    def getToughness(self):
        """获取角色韧性"""
        return self._toughness

    def setCriHarm(self, crih):
        """设置角色暴击伤害系数"""
        self._criHarm = crih

    def getCriHarm(self):
        """获取角色暴击伤害系数"""
        return self._criHarm

    def setFightSpeed(self, fightSpeed):
        """设置角色攻速"""
        self._fightSpeed = fightSpeed

    def getFightSpeed(self):
        """获取角色攻速"""
        return self._fightSpeed

    def updateFightPower(self, power):
        """更新战斗力"""
        self.setFightPower(power)
        # self._owner.SyncRoleFightPower()

        # pmmode = tb_character_admin.getObj(self._owner.baseInfo.getId())
        # pmmode.update('fightpower', self.getFightPower())

    def setFightPower(self, power):
        """设置角色战斗力"""
        self._fightPower = power

    def getFightPower(self):
        """获取角色战斗力"""
        return self._fightPower

    def setLuckAtk(self, luckAtk):
        """设置角色攻击幸运值"""
        if self._luckAtk > 40:
            self._luckAtk = 40
        else:
            self._luckAtk = luckAtk

    def getLuckAtk(self):
        """获取角色攻击幸运值"""
        return self._luckAtk

    def setLuckDef(self, luckDef):
        """设置角色诅咒值"""
        self._luckDef = luckDef

    def getLuckDef(self):
        """获取角色诅咒值"""
        return self._luckDef

    def setRank(self, rank):
        """设置角色竞技排名"""
        self._rank = rank

    def getRank(self):
        """获取角色竞技排名"""
        return self._rank

    def getCharacterAttr(self):
        """获取角色的所有属性"""
        info = {}
        return info

    def updateCharacterAttribute(self, attCombinationType=None):
        """更新角色属性数据"""
        if attCombinationType is None:
            attCombinationType = []
        for cleartAttConbination in attCombinationType:
            self.clearAttributeRecoder(cleartAttConbination)
        data = s_profession.datas.get(self.getProfession() * 1000 + self.getLevel(), {})
        attributeData = {}
        # attributeData = Utils.addDict(attributeData, self.getAllEquipAttributes())  # 装备
        # attributeData = Utils.addDict(attributeData, self.getAllGuildSkillAttributes())  # 公会技能
        # attributeData = Utils.addDict(attributeData, self.getCyLevelAttributes())  # 阶级
        # 附加强魂
        # attributeData = util.addDict(attributeData, self.pack.getEquipQiangHunAttributes())
        # if self.getLevel() >= ConstantDefine.SYSTEM_OPEN_LEVEL_GODBEAST:
        #     attributeData = util.addDict(attributeData, self.godBeast.getGodBeastAttributes())  # 神兽
        # if self.getLevel() >= ConstantDefine.SYSTEM_OPEN_LEVEL_WING and self.wing.getWingSkinId() > 0:
        #     attributeData = util.addDict(attributeData, self.wing.getWingAttributes())  # 翅膀
        # if self.level.getLevel() >= ConstantDefine.SYSTEM_OPEN_LEVEL_HLEPOUT:
        #     attributeData = util.addDict(attributeData, self.getHelpOutAddAttribute())  # 助阵属性

        if not data:
            # print 'no profession data
            # :%d'%self.profession.getProfession()*1000 + self.level.getLevel()
            return

        self.setHp(data['perLevelHp'] + attributeData.get('addHp', 0))  # 角色血量
        self.setMaxHp(data['perLevelHp'] + attributeData.get('addHp', 0))  # 角色血量
        self.setMp(data['preLevelMp'] + attributeData.get('addMp', 0))  # 角色魔法值
        self.setMaxMp(data['preLevelMp'] + attributeData.get('addMp', 0))  # 角色魔法值
        self.setAttackMin(data['perLevelAtkMin'] + attributeData.get('addAttack', 0))  # 角色物理攻击下限
        self.setAttackMax(data['perLevelAtkMax'] + attributeData.get('addAttackMax', 0))  # 角色物理攻击上限
        self.setMcAttackMin(data['perLevelMcAtkMin'] + attributeData.get('addMcAtk', 0))  # 角色的魔法攻击下限
        self.setMcAttackMax(data['perLevelMcAtkMax'] + attributeData.get('addMcAtkMax', 0))  # 角色的魔法攻击上限
        self.setScAttackMin(data['perLevelScAtkMin'] + attributeData.get('addScAtk', 0))  # 角色的道术攻击下限
        self.setScAttackMax(data['perLevelScAtkMax'] + attributeData.get('addScAtkMax', 0))  # 角色的道术攻击上限
        self.setAttackDefMin(data['perLevelDefAtkMin'] + attributeData.get('addDefAtk', 0))  # 角色的物理防御下限
        self.setAttackDefMax(data['perLevelDefAtkMax'] + attributeData.get('addDefAtkMax', 0))  # 角色的物理防御上限
        self.setMcAttackDefMin(data['perLevelDefMcAtkMin'] + attributeData.get('addDefMcAtk', 0))  # 角色的魔法防御下限
        self.setMcAttackDefMax(data['perLevelDefMcAtkMax'] + attributeData.get('addDefMcAtkMax', 0))  # 角色的魔法防御上限
        self.setHit(data['perLevelHit'] + attributeData.get('addHit', 0))  # 角色的命中
        self.setDodge(data['perLevelDog'] + attributeData.get('addDodge', 0))  # 角色的闪避几率
        self.setCri(data['perLevelCri'] + attributeData.get('addCri', 0))  # 角色的当前暴击率
        self.setToughness(data['perLevelTgs'] + attributeData.get('addToughness', 0))  # 角色的抗暴率
        self.setFightSpeed(attributeData.get('addFightSpeed', 0))  # 角色攻速
        self.setCriHarm(data['perLevelCriHarm'])  # 暴击系数
        self.setLuckAtk(attributeData.get('addAtcLuck', 0))

        # self.getAllPercentAttributes()  # 被动加成

        self.calculateFightPower()
        # self.matrix.calculateTeamFightPower()

    def getFightData(self, direction, isAthletics=False, isWorldBoss=False):
        """获取战斗数据"""
        fightdata = {}
        fightdata['chaId'] = self.baseInfo.id  # 角色的ID
        fightdata['chaName'] = self.baseInfo.getName()  # 角色的昵称
        fightdata['chaLevel'] = self.getLevel()  # 角色的等级
        # 角色的类型  1:玩家角色 2:怪物 3:宠物
        fightdata['characterType'] = 1
        # 角色职业
        fightdata['chaProfessionType'] = self.getProfession()
        fightdata['figureType'] = self.getFightFigure()  # 角色形象类型
        fightdata['chaBattleId'] = 2  # 角色在战场中的位置id
        # (角色在战斗中的归属阵营)1--(主动方)玩家朝向右，朝向右。2(被动方)--玩家朝向左
        fightdata['chaDirection'] = 1
        fightdata['luckAtk'] = self.getLuckAtk()  # 角色攻击幸运值
        fightdata['luckDef'] = self.getLuckDef()  # 角色诅咒值
        fightdata['chaCurrentHp'] = self.getMaxHp()  # 角色当前血量
        fightdata['chaMaxHp'] = self.getMaxHp()  # 角色的最大血量
        fightdata['chaCurrentMp'] = self.getMp()  # 角色当前魔法值
        # fightdata['chaCurrentMp'] = 50  # 角色当前魔法值
        fightdata['chaMaxMp'] = 100  # 角色的最大魔法值
        fightdata['physicalAttack'] = self.getAttackMin()  # 角色物理攻击下限
        # 角色物理攻击上限
        fightdata['physicalAttackMax'] = self.getAttackMax()
        fightdata['magicAttack'] = self.getMcAttackMin()  # 角色的魔法攻击下限
        # 角色的魔法攻击上限
        fightdata['magicAttackMax'] = self.getMcAttackMax()
        # 角色的物理防御下限
        fightdata['physicalDefense'] = self.getAttackDefMin()
        # 角色的物理防御上限
        fightdata['physicalDefenseMax'] = self.getAttackDefMax()
        # 角色的魔法防御下限
        fightdata['magicDefense'] = self.getMcAttackDefMin()
        # 角色的魔法防御上限
        fightdata['magicDefenseMax'] = self.getMcAttackDefMax()
        fightdata['scAttack'] = self.getScAttackMin()  # 角色的道术攻击下限
        fightdata['scAttackMax'] = self.getScAttackMax()  # 角色的道术攻击上限
        fightdata['hitRate'] = self.getHit()  # 角色的命中
        fightdata['critRate'] = self.getCri()  # 角色的当前暴击率
        fightdata['toughness'] = self.getToughness()  # 角色的抗暴率
        fightdata['fightSpeed'] = self.getFightSpeed()  # 角色的攻速
        fightdata['dodgeRate'] = self.getDodge()  # 角色的闪避几率
        fightdata['criHarm'] = self.getCriHarm()  # 角色的暴击系数
        fightdata['ActiveSkillList'] = []  # self.skillsAdorn.getSkillsAdornId(isAthletics)  # 角色的主动攻击技能
        fightRowFormat = {1: 1, 2: 2, 3: 3, 4: 1, 5: 2, 6: 3}
        fightdata['fightRow'] = fightRowFormat.get(2)  # 角色所在战斗行
        fightdata['canBeAttacked'] = 1  # 角色是否能被攻击
        fightdata['canAttacked'] = 1  # 角色是否能发动攻击
        fightdata['died'] = 0  # 是否死亡
        fightdata['canDoOrdSkill'] = 1  # 能否普通攻击
        fightdata['nextReleaseSkill'] = 0  # 下一条技能序号
        fightdata['canBeNotHurt'] = 0  # 是否免疫伤害
        fightdata['canBeDizziness'] = 0  # 是否能被眩晕
        # 普通攻击ID 10000000+职业*1000000
        fightdata['ordSkill'] = 10010101  # self.level.getCyLevelInfo().get('normalSkill', 0)
        # fightdata['ActiveSkillList'].extend([fightdata['ordSkill'], fightdata['ordSkill']])  # 增加两次普通攻击
        fightdata['hurtGodAdd'] = 0  # 神属性增加攻击值百分比
        fightdata['magicDefGodAdd'] = 0  # 神属性增加魔防百分比
        fightdata['criHurtGodAdd'] = 0  # 神属性增加暴击伤害值百分比
        fightdata['physicalDefGodAdd'] = 0  # 神属性增加物防百分比
        fightdata['absorbHpGodAdd'] = 0  # 神属性增加生命吸取百分比
        fightdata['dodgeGodAdd'] = 0  # 神属性增加闪避百分比
        fightdata['recoverHpGodAdd'] = 0  # 神属性增加恢复生命百分比
        fightdata['expGodAdd'] = 0  # 神属性增加获取经验百分比
        fightdata['buffEffects'] = {}  # BUFF效果
        fightdata['armor'] = 0  # 护甲
        fightdata['deHpRecover'] = 0  # 削弱生命恢复值
        fightdata['deHpRecoverPercent'] = 0  # 削弱生命恢复百分比
        fightdata['qiangHunHurtPercent'] = 0  # (self.pack.getEqipmentsStrengthenMaxMinLevel(
        # ) % 3) * dbItems.QIANHUN_HURT_PERCENT  # 强魂增伤百分比
        # self.events.calculateAttributeBuff(fightdata)  # 计算事件BUFF加成
        # self.calculateGodProperty(fightdata)  # 计算神属性
        # self.calculateWingProperty(fightdata)  # 计算翅膀特殊属性
        #         print isWorldBoss
        # if isWorldBoss:
        #     # 计算鼓舞加成
        #     CheerCounts = self.zhanyi.getWorldBossCheer()
        #
        #     if fightdata['chaProfessionType'] == dbuser.Profession_Soldier:
        #         # 角色物理攻击下限
        #         fightdata['physicalAttack'] += fightdata['physicalAttack'] * (0.1 * CheerCounts)
        #         # 角色物理攻击上限
        #         fightdata['physicalAttackMax'] += fightdata['physicalAttackMax'] * (0.1 * CheerCounts)
        #     elif fightdata['chaProfessionType'] == dbuser.Profession_Taoist:
        #         fightdata['scAttack'] += fightdata['scAttack'] * (0.1 * CheerCounts)  # 角色的道术攻击下限
        #         fightdata['scAttackMax'] += fightdata['scAttackMax'] * (0.1 * CheerCounts)  # 角色的道术攻击上限
        #     elif fightdata['chaProfessionType'] == dbuser.Profession_Mage:
        #         fightdata['magicAttack'] += fightdata['magicAttack'] * (0.1 * CheerCounts)  # 角色的魔法攻击下限
        #         # 角色的魔法攻击上限
        #         fightdata['magicAttackMax'] += fightdata['magicAttackMax'] * (0.1 * CheerCounts)

        return fightdata
